﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SeleniumOrderPathRunner {
    public class UserInputHandler {


        public enum SwitchCharacters {
            S, B, E, F, O
        };

        String ip;
        String brows;
        String env;
        String[] testCases;
        String[] orderpathList;

        public UserInputHandler() {
            ip = "";
            brows = "";
            env = "";
            testCases = new String[0];
            orderpathList = new String[0];
        }

        


        public String getManual() {
            String browsers = "";
            foreach (String browser in System.Enum.GetNames(typeof(SeleniumServer.Browser))) {
                browsers += System.Environment.NewLine + "*" + browser.ToLower();
            }
            String environments = "";
            foreach (String environment in System.Enum.GetNames(typeof(SeleniumServer.OrderAppEnvironment))) {
                environments += System.Environment.NewLine + environment.ToLower();
            }
            String functions = "";
            foreach (String function in System.Enum.GetNames(typeof(TestRunner.TestCaseGroups))) {
                functions += System.Environment.NewLine + function.ToLower();
            }

            return @"Usage:
            -s serverIP -b browser -e environment [-f functions] [-o orderpath names]


            Where:
            -s serverIP
                location of the machine running the selenium http proxy server, use localhost for your own machine

            -b browser
                the browser used to run the tests" + browsers + @"

            -e environment
                the application server used" + environments + @"

            -f functions
                the test case functions to be run. More than one can be used. Quote the list and comma separate each item" + functions + @"

            -o orderpath names
                the names of the orderpaths to be tested. More than one can be used. Quote the list and comma separate each item.";
        }






        public void parseInput(String[] args) {
            for (int i = 0; i < args.Length; i += 2) {

                if (args[i].StartsWith("-") && args[i].Length == 2) {
                    SwitchCharacters switchCharacter = (SwitchCharacters)Enum.Parse(typeof(SwitchCharacters), args[i].Substring(1, 1), true);
                    switch (switchCharacter) {
                        case SwitchCharacters.B:
                        if (brows.Equals("")) {
                            brows = args[i + 1];
                        } else {
                            throw new ArgumentException("duplicate switch");
                        }    
                        break;
                        case SwitchCharacters.E:
                        if (env.Equals("")) {
                            env = args[i + 1];
                        } else {
                            throw new ArgumentException("duplicate switch");
                        }
                        break;
                        case SwitchCharacters.F:
                        if (testCases.Length == 0) {
                            testCases = args[i + 1].Split(new char[] {','});
                        } else {
                            throw new ArgumentException("duplicate switch");
                        }
                        break;
                        case SwitchCharacters.S:
                        if (ip.Equals("")) {
                            ip = args[i + 1];
                        } else {
                            throw new ArgumentException("duplicate switch");
                        }
                        break;
                        case SwitchCharacters.O:
                        if (orderpathList.Length == 0) {
                            orderpathList = args[i + 1].Split(new char[] { ',' });
                        } else {
                            throw new ArgumentException("duplicate switch");
                        }
                        break;
                    }



                } else {
                    throw new ArgumentException("missing switch declaration");
                }

            }

            if (ip.Equals("") || env.Equals("") || brows.Equals("")) {
                throw new ArgumentException("missing necessary parameter");
            }

        }




        /// <summary>
        /// Kinda gross. Might want to rework the structure of the user input class. Don't want it under selServer though, prefer separation...
        /// </summary>
        /// <param name="serverIPAddress"></param>
        /// <param name="testClientBrowser"></param>
        /// <param name="testAppServerEnvironment"></param>
        /// <param name="testCaseGroup"></param>
        /// <param name="orderpathTestList"></param>
        public void setServerAndClientParams(out String serverIPAddress, out String testClientBrowser, out String testAppServerEnvironment, out String[] testCaseGroup, out String[] orderpathTestList ) {
            serverIPAddress = ip;
            testClientBrowser = brows;
            testAppServerEnvironment = env;
            testCaseGroup = testCases;
            orderpathTestList = orderpathList;
        }
    }
}
